<template>
  <div class="main">
    <div
      class="login-wrapper"
    >
      <h1>任务管理系统</h1>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        @submit.native.prevent="onSubmit"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-button"
            :class="{error: loginError}"
            type="success"
            native-type="submit"
            :loading="loading"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { mapActions } from 'vuex'
import {errorMessage} from '@/utils/helpers'
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      loginError: false
    }
  },
  methods: {
    ...mapActions(['login']),
    onSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          this.login({
            username: this.form.username,
            password: this.form.password
          })
            .then(data => {
              this.loading = false
              this.$router.push(this.$route.query.redirect || '/')
            })
            .catch(err => {
              this.$notify({
                title: '错误',
                message: errorMessage(err, '用户名或密码错误'),
                type: 'error',
                duration: 1500
              })
              this.loading = false
              this.loginError = true
              setTimeout(() => {
                this.loginError = false
              }, 500)
            })
        }
      })
    }
  }
}
</script>
<style lang="stylus">
@import '~@/assets/css/variable';

$input-width = 15rem;

html {
  height: 100%;
}

body {
  height: 100%;
}

.main {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  background-position: 100%;
  background-image: url('~@/assets/images/login-bg.jpg');
}

.login-wrapper {
  align-self: center;

  > h1 {
    position: relative;
    margin: 0 0 1rem;
    text-align: center;
    z-index: 1;
  }

  > form {
    width: $input-width;
    margin: 0 auto;
    padding-bottom: 111px;

    .el-input__inner {
      color: $color-black-exact-light;
      border-color: $color-silver-light;
      background-color: transparent;

      &:focus {
        color: $color-black;
        border-color: $color-black;
      }
    }
  }

  .login-button {
    width: 100%;

    &.error {
      animation: shake 0.5s;
    }
  }
}
</style>
